package com.example.teamtribe.main.chat.recent;


import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.example.teamtribe.Application;
import com.example.teamtribe.R;
import com.example.teamtribe.UpdateService;
import com.example.teamtribe.base.BaseFragment;
import com.example.teamtribe.database.ChattingBox;
import com.example.teamtribe.database.Request;
import com.example.teamtribe.database.TeamRequest;
import com.example.teamtribe.event.Unread;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

public class RecentFragment extends BaseFragment {

    public static final int REQUEST_REQU = 1;

    private List requests;
    private SmartRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private RecentAdapter adapter;
    private UpdateService.Binder binder;

    @Override
    protected int setContentView() {
        return R.layout.fragment_chat_recent;
    }

    @Override
    protected void onCreateView(View view) {
        binder = Application.getBinder();
        EventBus.getDefault().register(this);
        recyclerView = view.findViewById(R.id.rv_recent);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setNestedScrollingEnabled(false);
        requests = new ArrayList();
        adapter = new RecentAdapter(requests);
        recyclerView.setAdapter(adapter);
        refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                binder.getRequestMeg();
                binder.getResponseMeg();
                binder.getTeamResponse();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(Request request) {
        Log.d("接受事件", "好友消息接受的事件");
        initData();
        refreshLayout.finishRefresh();
    }

    private void initData() {
        requests.clear();
        requests.addAll(DataSupport.findAll(ChattingBox.class, true));
        requests.addAll(DataSupport.order("realId desc").find(Request.class));
        //requests.addAll(DataSupport.order("realId desc").find(TeamRequest.class));
        int num = 0;
        for (Object obj : requests) {
            if (obj instanceof  Request) {
                Request request = (Request) obj;
                if (request.isRead() == 0) {
                    num++;
                }
            } else if (obj instanceof  TeamRequest){
                TeamRequest request = (TeamRequest) obj;
                if (!request.isRead()) {
                    num++;
                }
            }
        }
        EventBus.getDefault().post(new Unread(2, num));
        adapter.notifyDataSetChanged();
    }
}
